// pages/master/mainten/famMainten/famMainten.js
const familyDB=wx.cloud.database().collection('FamilyInformationData')
var child = {}
var guard = {}
Page({

  /**
   * 页面的初始数据
   */
  data: {
      famnum:null,
      Guardian:null,
      children:null,
      isEdit:true,
      btn_txt:"编辑",
      childDate:null,
      guardDate:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options.num)
      familyDB.where({
        FamilyNum:options.num
      }).get().then(res=>{
        console.log(res.data[0])
        let children = res.data[0].FamilyMem.children
        let Guardian = res.data[0].FamilyMem.Guardian
        // this.setData({
        //   children:children,
        //   Guardian:Guardian,
        //   famnum:options.num
        // })
        for(var i in children){
          //console.log(i,children[i])
          child[i] = children[i].ChildBirth
        }
        for(var i in Guardian){
          guard[i] = Guardian[i].GdnBirth
        }
        console.log(child,guard)
        this.setData({
          children:children,
          Guardian:Guardian,
          famnum:options.num,
          childDate:child,
          guardDate:guard
        })
      })

      //家庭编号  儿童姓名  生病时间 生病原型  身体状况  
  },
  pickGdnBirth:function(e){
      console.log(e)
      let idx = e.currentTarget.dataset.idx
      let value = e.detail.value
      console.log(idx,value)
      guard[idx] = value
      console.log(guard)
      this.setData({
        guardDate:guard
      })
  },
  pickChildBirth:function(e){
    console.log(e)
      let idx = e.currentTarget.dataset.idx
      let value = e.detail.value
      console.log(idx,value)
      child[idx] = value
      console.log(child)
      this.setData({
        childDate:child
      })
  },
  clickEdit:function(e){
    let that = this
    if(that.data.btn_txt=="编辑"){
      that.setData({
        btn_txt:"保存",
        isEdit:false
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '确定保存信息？\n(将修改数据库数据)',
        success (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '正在保存...',
          })
         console.log(e.detail.value)
         var guarden = []
         var children = []
         var dataDic = e.detail.value
         var res = Object.keys(dataDic).sort()
         var i= 0
         var j= 0
         var datai={}
         var dataj={}
          for(var key in res){
            //let idx = res[key].slice(0,1)
            let kk = res[key].slice(1)
            if(res[key].slice(1,2)=='G'){
                // let idx = res[key].slice(0,1)
                // let kk = res[key].slice(1)
                i++
                datai[kk]  =  dataDic[res[key]]
                if(i>=9){
                  guarden.push(datai)
                  datai={}
                  i=0
                }
              //  console.log(idx,kk,dataDic[res[key]])
            }else if(res[key].slice(1,2)=='C'){
              j++
              dataj[kk]  =  dataDic[res[key]]
              if(j>=4){
                children.push(dataj)
                dataj={}
                j=0
              }
            }
            // console.log(guarden,children)
            // res[key].slice(0,1)
            // console.log(tt)
            // console.log("key: " + res[key] + " ,value: " + dataDic[res[key]]);
          }
          console.log(guarden,children)
          wx.cloud.callFunction({
            name:'updateFamiluyInfo',
            data:{
              Fnum:that.data.famnum,
              Children:children,
              Guardian:guarden
            },success: res => {
              wx.hideLoading()
              wx.showToast({  
                title: '修改成功',  
                icon: 'success',  
                duration: 2000  
            })   
          that.setData({
            btn_txt:"编辑",
            isEdit:true
          })}})
        } else if (res.cancel) {
        console.log('用户点击取消')
        }}
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})